package jwebtester.business.services.impl;

import infrastructure.Scopes;
import jwebtester.business.services.MapperService;

import org.dozer.DozerBeanMapper;
import org.dozer.MappingException;
import org.dozer.spring.DozerBeanMapperFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

@Service
@Scope(value = Scopes.Request, proxyMode = ScopedProxyMode.INTERFACES)
public class MapperServiceImpl implements MapperService {
	
	private static final Logger LOG = LoggerFactory.getLogger(MapperServiceImpl.class);

	@Autowired
	private transient DozerBeanMapperFactoryBean factory;

	
	@Override
	public Object mapTo(final Object source, final Class<?> destinationClass){
		DozerBeanMapper mapper = null;
		try {
			mapper = (DozerBeanMapper) factory.getObject();
			return mapper.map(source, destinationClass);
		} catch (Exception e) {
			LOG.error("Error mapping object(s)", e);
			throw new MappingException("Error mapping object(s)", e);
		}
	}
}
